
var hosDao = require('../dao/hospital.dao.js');
var docDao = require('../dao/doctor.dao.js');

module.exports.create = function(req, res) {
  //console.log('req.body='+JSON.stringify(req.body));
  var dataPkg = req.body;
  //console.log('dataPkg='+dataPkg);
  docDao.createDoctor(dataPkg.model,function(err,doctor){
    if (err) {
      console.log(err);
      res.status(406).json(err);
      return;
    }
 /*   var hosInfo = dataPkg.hosInfo;
    //console.log('userInfo',JSON.stringify(userInfo,null,2));
    if(hosInfo && doctor!=null){
      //fill hid to user here
      //*
      hosDao.appendDoc2Hos(hosInfo.hid,doctor._id,function(err,hospital){
        if(err){
          console.error(err);
          return;
        }
        console.log('appendHos2User ok');
      });//
    }*/
    res.status(201).json(doctor);
  });
};

module.exports.showList = function(req, res) {
  var filter = null;
  var type = req.params.type;
  console.log("type=" + type);
  if(type == "doctor"){
	  filter = {dtype:"doctor"};
  }
	if(type == "nurse"){
		filter = {dtype:"nurse"};
  }
  docDao.getDoctorList(filter,20,function(err,rows){
    console.log('rows.length=',rows.length);
    if (err) {
      console.log(err);
      res.status(400).json(err);
    }
    res.status(200).json(rows);
  });
};

module.exports.updateOne = function(req, res) {
  //console.log('api controllers===req.params.hid='+req.params.hid);
  docDao.updateDoctor(req.params.did,req.body,function(err,doctor){
    if (err) {
      console.log(err);
      res.status(406).json('update failed:'+err);
    }
    res.status(200).json(doctor);
  });
};

module.exports.deleteOne = function(req, res) {
  //console.log('api controllers===req.params.hid='+req.params.hid);
  var did = req.params.did;
  docDao.removeDoctor(did,function(err,did){
    if (err) {
      console.log(err);
      //res.status(404).json(err);
      res.status(500).json('doctor remove failed.'+err);
    }
    res.status(200).json(did);
  });
};

module.exports.readOne = function(req, res) {
  //console.log('api controllers.readOne===req.params.hid='+req.params.hid);
  var did = req.params.did;
  docDao.getDoctor(did,function(err,doctor){
    if (err) {
      console.log(err);
      res.status(404).json(err);
    }
    res.status(200).json(doctor);
  });
};

